#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
调用 RedisHandler 类删除 Redis 缓存的示例脚本
"""

from utils.cache_process.redis_control import RedisHandler
import socket


def main():
    # 创建 RedisHandler 实例
    redis_handler = RedisHandler()

    try:
        print(f"正在连接 Redis 服务器: {redis_handler.host}:{redis_handler.port}")

        # 测试网络连接
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.settimeout(5)
        result = sock.connect_ex((redis_handler.host, redis_handler.port))
        sock.close()

        if result != 0:
            print(f"网络连接失败: 无法连接到 {redis_handler.host}:{redis_handler.port}")
            return

        # 测试 Redis 连接
        if redis_handler.redis.ping():
            print("Redis 连接成功!")

            # 删除指定的缓存键
            cache_key_to_delete = "JOYHUB:rate_limit:post_user:10003065:feed"
            redis_handler.del_cache(cache_key_to_delete)
            print(f"已删除缓存键: {cache_key_to_delete}")
            # 示例2: 删除多个指定的缓存键
            # cache_keys_to_delete = ["user_info", "session_id", "temp_data"]
            # for key in cache_keys_to_delete:
            # redis_handler.del_cache(key)
            # print(f"已删除缓存键: {key}")

            # 示例3: 清理所有缓存数据（谨慎使用）
            # redis_handler.del_all_cache()
            # print("已清理所有缓存数据")
            print("缓存删除操作完成!")
        else:
            print("Redis 连接失败!")

    except Exception as e:
        print(f"操作过程中出现异常: {e}")




if __name__ == '__main__':
    main()
